package com.leyou.item.service.impl;

import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpecServiceImpl implements SpecService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;
    @Override
    public List<SpecGroupDTO> queryGroupByCategory(Long cid) {

        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> specGroups = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(specGroups)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
    }

    @Override
    public List<SpecParamDTO> querySpecParams(Long gid,Long cid,Boolean searching) {
        // gid和cid必选一个
        if (gid == null && cid == null) {
            throw new LyException(LyExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);

        List<SpecParam> specParams = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParams)){
            throw  new LyException(LyExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specParams,SpecParamDTO.class);
    }
//根据分类id查询规格组和规格参数
    @Override
    public List<SpecGroupDTO> querySpecGroupByCid(Long cid) {
        //基于分类查询规格组
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> groupList = specGroupMapper.select(specGroup);
        //基于分类id查询该分类下的规格参数
        List<SpecParamDTO> specParamDTOList = querySpecParams(null, cid, null);
        //基于规格组id对规格参数分组操作,long就是组Id,采用流 式编程对规格参数进行分组
        Map<Long, List<SpecParamDTO>> map = specParamDTOList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        List<SpecGroupDTO> groupDTOList = new ArrayList<>();
        //将查出的组进行类型转换(DTO),并给规格参数属性赋值
        for (SpecGroup group : groupList) {
            List<SpecParamDTO> paramDTOList = map.get(group.getId());
            SpecGroupDTO specGroupDTO = BeanHelper.copyProperties(group, SpecGroupDTO.class);
            specGroupDTO.setParams(paramDTOList);
            groupDTOList.add(specGroupDTO);
        }

        return groupDTOList;
    }
}
